// pages/comprehensivePolicy/comprehensivePolicy.js
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
		/* 机器人移动的位置 */
		leftLooks: 250,
		topLooks: 350,
		/* 列表margin */
		MarginTop:0,
		/* 表面浮层高度 */
		Height:0,
		/* 内容分类列表高度 */
		listHeight:0,
		/* 当前选择 */
		currIndex:-1,
		/* 地区列表下标 */
		regionIndex:-1,
		/* 地区字母下标 */
		letterIndex:0,
		/* 是否下拉刷新 */
		refresh:false,
		classifyList:[
			{
				name:"地区分类",
				src:"../../images/drop-down.png",
				transforms:""
			},
			{
				name:"内容分类",
				src:"../../images/drop-down.png",
				transforms:""
			},
			{
				name:"性质分类",
				src:"../../images/drop-down.png",
				transforms:""
			},
		],
		/* 地区字母 */
		regionLetter:[
			{name:"A",active:"active"},{name:"B",active:""},{name:"C",active:""},{name:"D",active:""},{name:"E",active:""},{name:"F",active:""},{name:"G",active:""},{name:"H",active:""},{name:"I",active:""},{name:"J",active:""},{name:"K",active:""},{name:"L",active:""},{name:"M",active:""},{name:"N",active:""},{name:"O",active:""},{name:"P",active:""},{name:"Q",active:""},{name:"R",active:""},{name:"S",active:""},{name:"T",active:""},{name:"U",active:""},{name:"V",active:""},{name:"W",active:""},{name:"X",active:""},{name:"Y",active:""},{name:"Z",active:""},{name:"#",active:""}
		],
		/* 地区 */
		regionList:[],
		/* 列表内容 */
		dynamic:[],
		/* 内容分类 */
		contentList:[],
		natureList:[],
		/* 手机宽高 */
		phoneHeight:0,
		phoneWidth:0,
		/* 列表高度 */
		newHeight:0,
		/* 来源类型 */
		type:"",
		/* 初始化页码 */
		page:1,
		/* 选择地区 */
		area:"",
		/* 父级id 2内容 3性质 */
		parentCategory:"",
		/* 底层类别 */
		childCategory:"",
		/* 内容选中id */
		choosedContentList:[],
		/* 性质选中id */
		choosedNatureList:[],
		/* 没有数据显示 */
		noneData:false,
		/* 判断parentCategory是否有值 */
		ischoosed:true,
		/* 1、保存 2、发布 3、转载  4、原创 */
		status:"2",
		/* 排列方式 */
		orderBy:"",
		/* 搜索内容 */
		inputValue:"",
		/* 创建多个自定义组件 */
		pages:[1],
		/* 当前页面的page值 */
		pageLength:1,
		/* 最多页码 */
		maxPage:2,
	},
	/* 跳转到问题页面 */
	toQuestion:function(){
		wx.navigateTo({
			url:"../questions/questions"
		})
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		if(options.type==11){
			wx.setNavigationBarTitle({ title: '综合政策' });
		}else if(options.type==13){
			wx.setNavigationBarTitle({ title: '政策解读' });
		}else if(options.type==14){
			wx.setNavigationBarTitle({ title: '立信洞察' });
		}
		wx.setStorageSync('maxPage',1);
		this.setData({
			type:options.type
		});
		wx.showShareMenu({  
			withShareTicket:true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
		var _this=this;
		var phoneHeight=0;
		/* 获取手机可视区域宽高 */
		wx.getSystemInfo({
			success: function(res) {
				phoneHeight=res.windowHeight;
				_this.data.phoneWidth=res.windowWidth;
				_this.data.phoneHeight=res.windowHeight;
			},
		});
		
		wx.createSelectorQuery().selectAll('.search-fixed').boundingClientRect(function (rect) {
			_this.setData({
				MarginTop:rect[0].height,
				Height:phoneHeight-rect[0].height,
				newHeight:phoneHeight-rect[0].height,
			})
		}).exec();
		this.queryDataByParentId();
  },
	/* 搜索内容 */
	searchValue:function(e){
		this.setData({
			inputValue:e.detail.value
		})
	},
	/* 搜索列表 */
	searchContent:function(){
		this.setData({
			pages:[],
			pageLength:0,
		})
		wx.setStorageSync('maxPage',1);
		this.queryArticle();
	},
	/* 查询分类Id */
	queryDataByParentId:function(){
		var _this=this;
		/* 查地区 */
		wx.request({
			url: util.url+"/system/queryDataByParentId/1",
			data: {},
			header: {
				'content-type': 'application/json' 
			},
			success(res) {
				if(res.data.resultCode==0){
					_this.setData({
						regionList:res.data.resultData
					})
				}else{
					wx.showToast({
						icon:"none",
						title:res.data.resultMsg
					})
				}
			}
		});
		/* 查内容 */
		wx.request({
			url: util.url+"/system/queryDataByParentId/2",
			data: {},
			header: {
				'content-type': 'application/json' 
			},
			success(res) {
				if(res.data.resultCode==0){
					var contentList=res.data.resultData;
					for(var i=0;i<contentList.length;i++){
						contentList[i].choosed="";
					}
					_this.setData({
						contentList:contentList
					})
				}else{
					wx.showToast({
						icon:"none",
						title:res.data.resultMsg
					})
				}
			}
		});
		/* 查性质 */
		wx.request({
			url: util.url+"/system/queryDataByParentId/3",
			data: {},
			header: {
				'content-type': 'application/json' 
			},
			success(res) {
				if(res.data.resultCode==0){
					var natureList=res.data.resultData;
					for(var i=0;i<natureList.length;i++){
						natureList[i].choosed="";
					}
					_this.setData({
						natureList:natureList
					})
				}else{
					wx.showToast({
						icon:"none",
						title:res.data.resultMsg
					})
				}
			}
		});
	},

	/* 查询最新动态 */
	queryArticle:function(){
		var _this=this;
		if(wx.getStorageSync('maxPage')>this.data.pageLength){
			this.data.pageLength+=1;
			this.data.pages.push(this.data.pageLength);
			if(this.data.type==11){
				this.data.orderBy="file_date";
			}else if(this.data.type==13){
				this.data.status="";
				this.data.orderBy="file_date";
			}else if(this.data.type==14){
				this.data.orderBy="publish_at";
			}
			this.setData({
				pages:this.data.pages,
				noneData:false,
				status:this.data.status,
				orderBy:this.data.orderBy,
				type:this.data.type,
				area:this.data.area,
				parentCategory:this.data.parentCategory,
				childCategory:this.data.childCategory,
				searchContent:this.data.inputValue
			})
			/* 保存最新的最大页码数 */
			var timer=setInterval(function(){
				if(wx.getStorageSync('newPage')){
					_this.data.maxPage=wx.getStorageSync('maxPage');
					wx.setStorageSync('newPage',false);
					clearInterval(timer);
				}
			},500);
		}else{
			this.setData({
				noneData:true
			})
			
		}
		
	},
	
	/* 滑动机器人 */
	imagetouchmove:function(e){
		var phoneWidth=this.data.phoneWidth;
		var phoneHeight=this.data.phoneHeight;
		var robotWidth=wx.getStorageSync("robotWidth");
		var robotHeight=wx.getStorageSync("robotHeight");
		e.touches[0].clientX=e.touches[0].clientX<0?0:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY<0?0:e.touches[0].clientY;
		e.touches[0].clientX=e.touches[0].clientX>phoneWidth-robotWidth?phoneWidth-robotWidth:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY>phoneHeight-robotHeight?phoneHeight-robotHeight:e.touches[0].clientY;
		
    this.setData({
      leftLooks: e.touches[0].clientX,
      topLooks: e.touches[0].clientY
    })
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
		if(!this.data.noneData){
			wx.setStorageSync('newPage',false);
			if(this.data.maxPage>-1){
				wx.setStorageSync('maxPage',this.data.maxPage);
			}
			this.queryArticle();
		}
  },

 
	/* 切换分类 */
	changeItem(e){
		var _this=this;
		if(e.currentTarget.dataset.transforms){
			_this.data.classifyList[e.currentTarget.dataset.index].transforms="",
			this.setData({
				currIndex:-1,
				classifyList:_this.data.classifyList
			});
		}else{
			_this.data.classifyList[e.currentTarget.dataset.index].transforms="transforms",
			this.setData({
				currIndex:e.currentTarget.dataset.index,
				classifyList:_this.data.classifyList
			});
			
			if(e.currentTarget.dataset.index){
				wx.createSelectorQuery().selectAll('.content-btn').boundingClientRect(function (rect) {
					_this.setData({
						listHeight:_this.data.Height-rect[0].height
					})
				}).exec();
			}
		}
	},
	/* 点击区域 */
	clickRegion:function(e){
		this.setData({
			regionIndex:e.currentTarget.dataset.index,
			currIndex:-1,
			area:e.currentTarget.dataset.area,
			pages:[],
			pageLength:0,
		});
		wx.setStorageSync('maxPage',1);
		this.queryArticle();
	},
	/* 点击区域字母 */
	clickLetter:function(e){
		this.setData({
			letterIndex:e.currentTarget.dataset.index,
		});
	},
	/* 点击内容分类 */
	clickContent:function(e){
		this.data.contentList[e.currentTarget.dataset.index].choosed=this.data.contentList[e.currentTarget.dataset.index].choosed?"":"choosed";
		if(this.data.contentList[e.currentTarget.dataset.index].choosed){
			this.data.choosedContentList.push(e.currentTarget.dataset.id);
			this.data.ischoosed=true;
		}else{
			for(var i=0;i<this.data.choosedContentList.length;i++){
				if(this.data.choosedContentList[i]==e.currentTarget.dataset.id){
					this.data.choosedContentList.splice(i,1);
				}
			}
			if(!this.data.choosedContentList.length){
				this.data.ischoosed=false;
				if(this.data.parentCategory&&this.data.parentCategory.indexOf("3")>-1){
					this.data.parentCategory="3";
				}else{
					this.data.parentCategory="";
				}
			}
		}
		this.setData({
			contentList:this.data.contentList,
			choosedContentList:this.data.choosedContentList,
			ischoosed:this.data.ischoosed
		});
	},
	/* 内容分类重置 */
	contentReset:function(){
		for(var i=0;i<this.data.contentList.length;i++){
			this.data.contentList[i].choosed="";
		}
		if(this.data.parentCategory&&this.data.parentCategory.indexOf("3")>-1){
			this.data.parentCategory="3";
		}else{
			this.data.parentCategory="";
		}
		this.setData({
			contentList:this.data.contentList,
			choosedContentList:[],
			ischoosed:false
		})
	},
	/* 内容分类确定 */
	contentSure:function(){
		this.calculate(2);
	},
	/* 点击性质分类 */
	clickNature:function(e){
		this.data.natureList[e.currentTarget.dataset.index].choosed=this.data.natureList[e.currentTarget.dataset.index].choosed?"":"choosed";
		if(this.data.natureList[e.currentTarget.dataset.index].choosed){
			this.data.choosedNatureList.push(e.currentTarget.dataset.id);
			this.data.ischoosed=true;
		}else{
			for(var i=0;i<this.data.choosedNatureList.length;i++){
				if(this.data.choosedNatureList[i]==e.currentTarget.dataset.id){
					this.data.choosedNatureList.splice(i,1);
				}
			}
			if(!this.data.choosedNatureList.length){
				this.data.ischoosed=false;
				if(this.data.parentCategory&&this.data.parentCategory.indexOf("2")>-1){
					this.data.parentCategory="2";
				}else{
					this.data.parentCategory="";
				}
			}
		}
		this.setData({
			natureList:this.data.natureList,
			choosedNatureList:this.data.choosedNatureList,
			ischoosed:this.data.ischoosed
		});
		
	},
	/* 性质分类重置 */
	natureReset:function(){
		for(var i=0;i<this.data.natureList.length;i++){
			this.data.natureList[i].choosed="";
		}
		if(this.data.parentCategory&&this.data.parentCategory.indexOf("2")>-1){
			this.data.parentCategory="2";
		}else{
			this.data.parentCategory="";
		}
		this.setData({
			natureList:this.data.natureList,
			choosedNatureList:[],
			ischoosed:false
		})
	},
	/* 性质分类确定 */
	natureSure:function(){
		this.calculate(3);
		
	},
	/* 计算 parentCategory和childCategory的值*/
	calculate:function(type){
		if(type==2){
			if(!this.data.parentCategory&&this.data.ischoosed){
				this.data.parentCategory="2";
			}else if(this.data.parentCategory&&this.data.ischoosed&&this.data.parentCategory.indexOf("2")==-1){
				this.data.parentCategory+=",2";
			}
		}else if(type==3){
			if(!this.data.parentCategory&&this.data.ischoosed){
				this.data.parentCategory="3";
			}else if(this.data.parentCategory&&this.data.ischoosed&&this.data.parentCategory.indexOf("3")==-1){
				this.data.parentCategory+=",3";
			}
		}
		this.data.childCategory="";
		this.data.childCategory+=this.data.choosedContentList.length?this.data.choosedContentList.join(",")+",":"";
		this.data.childCategory+=this.data.choosedNatureList.length?this.data.choosedNatureList.join(",")+",":"";
		if(this.data.childCategory.length){
			this.data.childCategory=this.data.childCategory.substring(0,this.data.childCategory.length-1);
		}
		this.setData({
			currIndex:-1,
			parentCategory:this.data.parentCategory,
			childCategory:this.data.childCategory,
			pages:[],
			pageLength:0,
		});
		wx.setStorageSync('maxPage',1);
		this.queryArticle();
	},
	
	/**
	* 页面相关事件处理函数--监听用户下拉动作
	*/
	onPullDownRefresh: function () {
		this.setData({
			pages:[],
			pageLength:0,
			refresh:true,
		})
		wx.setStorageSync('maxPage',1);
		this.queryArticle();
		wx.showNavigationBarLoading() //在标题栏中显示加载
	},
	/**
		 * 用户点击右上角分享朋友
		 */
		onShareAppMessage: function (e) {
			
		},
		/**
		* 用户点击右上角分享朋友圈
		*/
		onShareTimeline: function () {
	
		 },
})